<?php

namespace Module\DataManager\Admin\Controller;

use Illuminate\Routing\Controller;
use ModStart\Admin\Auth\AdminPermission;
use ModStart\Admin\Concern\HasAdminGrid;
use ModStart\Admin\Layout\AdminDialogPage;
use ModStart\Admin\Model\Data;
use ModStart\Core\Input\Response;
use ModStart\Core\Util\CRUDUtil;
use ModStart\Core\Util\FileUtil;
use ModStart\Data\AbstractDataStorage;
use ModStart\Data\DataManager;
use ModStart\Field\AbstractField;
use ModStart\Field\AutoRenderedFieldValue;
use ModStart\Field\File;
use ModStart\Field\Image;
use ModStart\Field\Video;
use ModStart\Form\Form;
use ModStart\Grid\Grid;
use ModStart\Grid\GridFilter;
use ModStart\ModStart;
use ModStart\Widget\ButtonDialogRequest;
use Module\DataManager\Type\DataCategory;

class IndexController extends Controller
{
    use HasAdminGrid;

    public function grid()
    {
        ModStart::js('asset/common/clipboard.js');
        $grid = Grid::make(Data::class);
        $grid->radio('category', '类别')->optionType(DataCategory::class)->width(100);
        $grid->display('_preview', '预览')->hookRendering(function (AbstractField $field, $item, $index) {
            $url = '/' . AbstractDataStorage::DATA . '/' . $item->category . '/' . $item->path;
            if (!empty($item->domain)) {
                $url = $item->domain . $url;
            }
            return AutoRenderedFieldValue::makeView('module::DataManager.View.preview', [
                'item' => $item,
                'url' => $url,
            ]);
        });
        $grid->display('filename', '文件名');
        $grid->display('size', '大小')->width(100)->hookRendering(function (AbstractField $field, $item, $index) {
            return AutoRenderedFieldValue::make(FileUtil::formatByte($item->size));
        });
        $grid->datetime('created_at', '上传时间');
        $grid->gridFilter(function (GridFilter $filter) {
            $filter->eq('category', '类别')->radio(DataCategory::class);
            $filter->like('filename', '文件名');
        });
        $grid->gridOperateAppend(
            ButtonDialogRequest::primary('<i class="iconfont icon-upload"></i> 上传文件', modstart_admin_url('data_manager/upload'))
        );
        $grid->canBatchSelect(true)->canBatchDelete(true);
        $grid->disableCUD()->canDelete(true)->canShow(false);
        $grid->title('附件管理');
        return $grid;
    }

    public function delete()
    {
        AdminPermission::demoCheck();
        foreach (CRUDUtil::ids() as $id) {
            DataManager::deleteById($id);
        }
        return Response::redirect(CRUDUtil::jsGridRefresh());
    }

    public function upload(AdminDialogPage $page)
    {
        $form = Form::make('');
        $form->radio('category', '类别')
            ->optionType(DataCategory::class)->required()->defaultValue(DataCategory::IMAGE)
            ->when(DataCategory::IMAGE, function (Form $form) {
                $form->image('imageFile', '图片')->mode(Image::MODE_RAW)->required();
            })
            ->when(DataCategory::FILE, function (Form $form) {
                $form->file('fileFile', '文件')->mode(File::MODE_RAW)->required();
            })
            ->when(DataCategory::VIDEO, function (Form $form) {
                $form->video('videoFile', '视频')->mode(Video::MODE_RAW)->required();
            });
        $form->showReset(false)->showSubmit(false);
        ModStart::style('[data-submit]{display:none !important;}');
        ModStart::script(<<<JS
$('[name=imageFile],[name=fileFile],[name=videoFile]').on('success',function(e,d){
    parent.__grids.get(0).lister.refresh();
    __dialogClose();
});
JS
        );
        return $page
            ->pageTitle('上传文件')
            ->body($form);
    }
}
